package com.example.zhaohang.employmentapp;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.zhaohang.been.MesBeen;
import com.example.zhaohang.contect.UriContect;
import com.example.zhaohang.utils.GsonUtil;
import com.example.zhaohang.utils.ToastMake;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;

public class ChengPawActivity extends BasicActivity implements View.OnClickListener {

    private TextView mTishi;
    private EditText mName3;
    private Button mName5;
    private Button mName8;
    private String phoneNumber;
    private int mTime;
    private TimerTask mWait;
    private Timer timer = new Timer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_cheng_paw);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTitle_V("更换登录密码");

        mTishi = (TextView) findViewById(R.id.tishi);
        mName3 = (EditText) findViewById(R.id.name3);
        mName5 = (Button) findViewById(R.id.name5);
        mName8 = (Button) findViewById(R.id.name8);

        mName5.setOnClickListener(this);
        mName8.setOnClickListener(this);
        phoneNumber = MyApplayCation.phone;

        String start = phoneNumber.substring(0, 3);
        String end = phoneNumber.substring(phoneNumber.length() - 3, phoneNumber.length());
        String text = start + "xxxx" + end;

        mTishi.setText(text);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //获取验证码
            case R.id.name5:
                if (isConnected(ChengPawActivity.this)){
                    haveCode();
                }
                break;
            case R.id.name8:
                String trim = mName3.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    skipOtherActivity(ChengPawTwoActivity.class, false);
                }
                break;
        }
    }

    private void haveCode() {

        mTime = 60;
        mWait = new TimerTask() {
            @Override
            public void run() {
                //再主线程中进行切换
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mTime <= 0) {

                            mName5.setText(getResources().getString(R.string.name19));
                            mName5.setEnabled(true);
                            mWait.cancel();
                        } else {

                            mName5.setText(mTime + "秒后重获");
                            mName5.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                        }
                        mTime--;

                    }
                });

            }
        };
        timer.schedule(mWait, 0, 1000);
        Map<String, String> map = new HashMap<>();
        map.put("newphone", MyApplayCation.phone);
        final String json = GsonUtil.parseMapToJson(map);
        new Thread(new Runnable() {
            @Override
            public void run() {
                showProgrees("提交中,请稍等...");
                OkHttpUtils
                        .postString()
                        .url(UriContect.SMS)
                        .content(json)
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                ToastMake.showToast(getResources().getString(R.string.name18));
                                hideProgree();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                hideProgree();
                                MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);
                                if (mesBeen == null) {
                                    ToastMake.showToast(getResources().getString(R.string.name18));
                                    mName5.setText(getResources().getString(R.string.name19));
                                    mName5.setEnabled(true);
                                    mWait.cancel();

                                    return;
                                }
                                if (!TextUtils.equals(mesBeen.getStatus(), "1")) {
                                    ToastMake.showToast(mesBeen.getMsg());
                                    mName5.setText(getResources().getString(R.string.name19));
                                    mName5.setEnabled(true);
                                    mWait.cancel();
                                    return;
                                } else {
                                    mWait.cancel();
                                    ToastMake.showToast(mesBeen.getMsg());
                                    MyApplayCation.registCode = mesBeen.getCode();
                                    //修改完成结束
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                           skipOtherActivity(ChengPawTwoActivity.class,false);
                                        }
                                    });

                                }

                            }
                        });
            }
        }).start();


    }
}
